//---------------------------------------------------------------------
// File: EntLibDatabaseDeleteStep.cs
// 
// Summary: 
//---------------------------------------------------------------------


using System.IO;
using System.Xml;
using BizUnitExtensions.Utilities;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions.BizTalk
{
    /// <summary>
    /// The MapExecutingStep can be used to execute a map and test the output from it
    /// </summary>
    /// 
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step. The step 
    /// can perform a transformation using a BizTalk map and output that to a directory
    /// 
    /// <code escaped="true">
    ///	<TestStep assemblyPath="BizUnitExtensions.BizTalk.dll" typeName="BizUnit.Extensions.BizTalk.MapExecutingStep">
    ///		<Map assemblyPath="C:\Program Files\Microsoft BizTalk Server 2006\SDK\Samples\Messaging\CBRSample\bin\Development\CBRSample.dll" 
    ///         typeName="CBRSample.CBRInput2CANMap"/>
    ///		<SourceDirectory>.\Rec_03</SourceDirectory>
    ///		<SearchPattern>CBRInput_*.xml</SearchPattern>
    ///		<DestinationDirectory>.\Rec_04</DestinationDirectory>
    ///     <DestinationNamePrefix>CBRSample</DestinationNamePrefix>
    ///	</TestStep>
    ///	</code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>Map</term>
    ///			<description>This is the BizTalk map that you wish to execute</description>
    ///		</item>
    ///     <item>
    ///			<term>SourceDirectory</term>
    ///			<description>This is the directory to pick up the test file</description>
    ///		</item>
    ///		<item>
    ///			<term>Pattern</term>
    ///			<description>Pattern for file names to be transformed</description>
    ///		</item>
    ///		<item>
    ///			<term>DestinationDirectory</term>
    ///			<description>Output directory</description>
    ///		</item>
    ///     <item>
    ///			<term>DestinationNamePrefix</term>
    ///			<description>Prefix for output files. All output files will be saved as .xml</description>
    ///		</item>
    ///	</list>
    ///	</remarks>
    public class MapExecutingStep : ITestStep
    {
        public void Execute(XmlNode testConfig, Context context)
        {
            string sourcePath = context.ReadConfigAsString(testConfig, "SourceDirectory");
            string pattern = context.ReadConfigAsString(testConfig, "SearchPattern");
            string destinationPath = context.ReadConfigAsString(testConfig, "DestinationDirectory");
            string destinationNamePrefix = context.ReadConfigAsString(testConfig, "DestinationNamePrefix");
            string mapAssemblyPath = context.ReadConfigAsString(testConfig, "Map/@assemblyPath");
            string mapTypeName = context.ReadConfigAsString(testConfig, "Map/@typeName");
            
            string[] filelist = Directory.GetFiles(sourcePath, pattern);

            XmlDocument outputDocument = new XmlDocument();
            XmlDocument inputDocument = new XmlDocument();
            string outputString;
            string filePath;
            int i=0;

            foreach (string file in filelist)
            {
                inputDocument.Load(file);
                if (mapAssemblyPath != null && mapAssemblyPath.Length != 0)
                {
                   outputString = MapHelper.TransformMessage(inputDocument.OuterXml, mapTypeName, mapAssemblyPath);
                }
                else
                {
                    outputString = MapHelper.TransformMessage(inputDocument.OuterXml, mapTypeName);    
                }
                outputDocument.LoadXml(outputString);
                filePath = destinationPath + "\\" + destinationNamePrefix + i.ToString() + ".xml"; 
                outputDocument.Save(filePath);
                context.LogInfo("MapExecutingStep has transformed file: \"{0}\" output is saved in \"{1}\"", file, filePath);
                i++;
            }
        }
    }
}